package com.ing5.BL.android.cinece.api.client;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.ing5.BL.android.cinece.api.model.Information;
import com.ing5.BL.android.cinece.api.model.SearchResult;
import com.ing5.BL.android.cinece.api.model.SearchResults;

public class JsonParser {
	
	//Json key names
	private final String TAG_SEARCH = "Search";
	private final String TAG_TITLE = "Title";
	private final String TAG_YEAR = "Year";
	private final String TAG_RATED = "Rated";
	private final String TAG_RELEASED = "Released";
	private final String TAG_RUN = "Runtime";
	private final String TAG_GENRE = "Genre";
	private final String TAG_DIRECTOR = "Director";
	private final String TAG_WRITER = "Writer";
	private final String TAG_ACTORS = "Actors";
	private final String TAG_PLOT = "Plot";
	private final String TAG_POSTER = "Poster";
	private final String TAG_RATING = "imdbRating";
	private final String TAG_VOTES = "imdbVotes";
	private final String TAG_ID = "imdbID";
	private final String TAG_TYPE = "Type";
	
	
	public JsonParser() {}
	
	public <T> T parseJson(String result, Class<T> resultType) {
		T object = null;
		
		if(!result.isEmpty()) {
			try {
				JSONObject jobj = new JSONObject(result);
				if(resultType.getSimpleName().equals("SearchResults"))
				{
					SearchResults searchResults = new SearchResults();
					List<SearchResult> listResults = new ArrayList<SearchResult>();
					
					JSONArray jarray = jobj.getJSONArray(TAG_SEARCH);
					for(int i=0; i<jarray.length(); i++) {
						JSONObject iobj = jarray.getJSONObject(i);						
						SearchResult oneresult = new SearchResult();
						
						oneresult.setTitle(iobj.getString(TAG_TITLE));
						oneresult.setYear(iobj.getString(TAG_YEAR));
						oneresult.setId(iobj.getString(TAG_ID));
						oneresult.setType(iobj.getString(TAG_TYPE));
						
						listResults.add(oneresult);
					}
					
					searchResults.setResultList(listResults);
					object = (T) searchResults;
				}
				else if(resultType.getSimpleName().equals("Information"))
				{
					Information info = new Information();
					
					info.setTitle(jobj.getString(TAG_TITLE));
					info.setYear(jobj.getString(TAG_YEAR));
					info.setRated(jobj.getString(TAG_RATED));
					info.setReleased(jobj.getString(TAG_RELEASED));
					info.setRuntime(jobj.getString(TAG_RUN));
					info.setGenre(jobj.getString(TAG_GENRE));
					info.setDirector(jobj.getString(TAG_DIRECTOR));
					info.setWriter(jobj.getString(TAG_WRITER));
					info.setActors(jobj.getString(TAG_ACTORS));
					info.setPlot(jobj.getString(TAG_PLOT));
					info.setImageUrl(jobj.getString(TAG_POSTER));
					info.setRating(jobj.getString(TAG_RATING));
					info.setVotes(jobj.getString(TAG_VOTES));
					info.setId(jobj.getString(TAG_ID));
					info.setType(jobj.getString(TAG_TYPE));
					
					object = (T) info;
				}
			} catch (JSONException e) {
				Log.e("ERROR", "Erreur 1 - JSon Parsing: " + e);
				object = null;
			}
		}
		return object;
	}
}
